﻿Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration
Imports PerformanceLogic
Imports System.Linq

Public Class CPUTimerJob
    Inherits Microsoft.SharePoint.Administration.SPJobDefinition

    Public Sub New()
        MyBase.New()

    End Sub
    Public Sub New(ByVal _Name As String, ByRef anApp As SPWebApplication, ByRef _JobLockType As SPJobLockType)


        MyBase.New(_Name, anApp, Nothing, _JobLockType)
        MyBase.Title = "Check CPU Use Timer Job"

    End Sub




    Public Overrides Sub Execute(ByVal targetInstanceId As System.Guid)
        ' MyBase.Execute(targetInstanceId)

        Dim allCPUData As New List(Of CPUMeasurementTest)

        Dim aServiceCollection As SPServiceCollection = SPFarm.Local.Services


        Dim aWebApp As SPWebApplication

        Try
            'Here we are determining which web application will store the Performance Data, this has to be the entral administration web application


            If Not SPWebService.AdministrationService.Status = SPObjectStatus.Disabled Then
                Dim anEnum As IEnumerator = SPWebService.AdministrationService.WebApplications.GetEnumerator()

                While anEnum.MoveNext


                    aWebApp = CType(anEnum.Current, SPWebApplication)
                    Exit While

                End While

            End If



            'here we bring back the performance data and serialize back into objects
            Dim CPUData As String
            Dim allProperties As Hashtable = aWebApp.Properties

            If allProperties.ContainsKey("CPUData") = True Then

                If allProperties("CPUData").ToString.Trim() <> "" Then
                    CPUData = allProperties("CPUData").ToString()
                    allCPUData = PerformanceLogic.CPUTestLogic.ParseCPUData(allProperties("CPUData").ToString())
                Else
                    CPUData = Nothing
                    allCPUData = New List(Of CPUMeasurementTest)

                End If

            Else
                allProperties.Add("CPUData", "")
                CPUData = Nothing
                allCPUData = New List(Of CPUMeasurementTest)
                aWebApp.Update()

            End If


            'So we have all CPU Data, let's perform a test to get the next set
            'First Bring back servers to test

            Dim ServerList As List(Of String) = PerformanceLogic.CPUTestLogic.DetermineServersToTest(SPFarm.Local)

            ' For Each Server Perform CPU Test


            For Each aServer As String In ServerList
                Dim aCPUTest As New CPUMeasurementTest()
                aCPUTest.PerformCPUTest(aServer)
                allCPUData.Add(aCPUTest)

            Next
            'Anything older than 2 days must be gone
            allCPUData = TrimOlderTests(allCPUData)

            'Store the Test Result
            allProperties("CPUData") = PerformanceLogic.CPUTestLogic.SaveCPUData(allCPUData)
            aWebApp.Update()


        Catch ex As Exception

            EventLog.WriteEntry("CPUUse", ex.Message & ":" & ex.StackTrace)
            Throw (ex)




        End Try


    End Sub


    Private Function TrimOlderTests(ByVal allCPUData As List(Of CPUMeasurementTest)) As List(Of CPUMeasurementTest)

        Dim CutoffDate As DateTime = System.DateTime.Now.AddDays(-2)

        Dim newCPUList As New List(Of CPUMeasurementTest)
        For Each aCPUTest As CPUMeasurementTest In allCPUData

            If aCPUTest.MeasureMentDate >= CutoffDate Then

                newCPUList.Add(aCPUTest)


            End If



        Next


        Return newCPUList



    End Function

End Class

